<template>
    <div class="orderDetailst-warp content-wrapp">
        <div class="left">
            <el-form :model="ruleForm" ref="ruleForm" :rules="rules">
                <div class="name">{{$t('m.label.orderNumber')}}</div>
                <el-form-item prop="amzOrderId">
                    <el-input
                        type="textarea"
                        :autosize="{ minRows: 10, maxRows: 15}"
                        :placeholder="$t('m.placeholder.pleaseEnteOrderNumber')"
                        v-model="ruleForm.amzOrderId"
                    ></el-input>
                </el-form-item>
                <el-button
                    type="primary"
                    @click="searchfor"
                    icon="el-icon-search"
                >{{$t('m.button.search')}}</el-button>
                <el-button
                    @click="ruleForm.amzOrderId = ''"
                    icon="el-icon-circle-close"
                >{{$t('m.button.reset')}}</el-button>
            </el-form>
        </div>
        <div class="right">
            <div class="name" v-if="itemData.length !== 0">{{$t('m.label.orderDetails')}}</div>
            <div v-loading="loading">
                <div v-if="itemData.length === 0" class="null">{{txt}}</div>
                <div class="content" v-for="(item,index) in itemData" :key="index">
                    <p>{{$t('m.label.account')}}：{{ item.brandName }}</p>
                    <p>{{$t('m.label.country')}}：{{ item.countryName }}</p>
                    <p>{{$t('m.label.orderNumber')}}：{{ item.orderId }}</p>
                    <p>{{$t('m.label.dateOfPurchase')}}：{{ item.cnFormatOrderDate}}</p>
                    <p>{{$t('m.label.buyerName')}}：{{ item.personName }}</p>
                    <p class="title">{{$t('m.label.orderProduct')}}</p>
                    <p v-for="(item,index) in item.orderItems" :key="index">
                        <span>{{$t('m.label.SKU')}}：{{item.productModelNumber}}</span>
                        <span>{{$t('m.label.ASIN')}}：{{item.asin}}</span>
                        <span>quantity：{{item.quantity}}</span>
                        <span>Price：{{item.unitPriceStr}}</span>
                    </p>
                    <p>
                        <span>{{$t('m.label.totalOrderPrice')}}：{{item.subtotalStr}}</span>
                        <span>{{$t('m.button.reimburse')}}：{{item.returnTotal}}</span>
                    </p>
                    <p class="title">{{$t('m.label.address')}}</p>
                    <p>
                        {{item.item}}
                        <br v-if="item.item" />
                        {{item.city}}
                        <br v-if="item.city" />
                        {{item.StateOrRegion}}{{item.postalCod}}
                        <br v-if="item.StateOrRegion || item.postalCod" />
                        {{item.countryCode}}
                    </p>
                </div>
            </div>
        </div>
    </div>
</template>     
<script>
const ERR_OK = 200;
export default {
    data() {
        return {
            txt: this.$t("m.message.noMoreData"),
            ruleForm: {
                amzOrderId: ""
            },
            loading: false,
            itemData: [],
            rules: {
                amzOrderId: [
                    {
                        required: true,
                        message: this.$t("m.placeholder.pleaseEnteOrderNumber"),
                        trigger: "blur"
                    }
                ]
            }
        };
    },
    mounted() {
        if (this.$route.params.orderId) {
            this.ruleForm.amzOrderId = this.$route.params.orderId
            this.searchfor()
        }
    },
    methods: {
        searchfor() {
            this.$refs["ruleForm"].validate(valid => {
                if (valid) {
                    this.txt = this.$t("m.message.loading");
                    this.loading = true;
                    this.itemData = [];
                    setTimeout(() => {
                        var amzOrderId = this.ruleForm.amzOrderId
                            .replace(/\ +/g, "<br>")
                            .replace(/[\r\n]/g, "<br>")
                            .split(/[/<br \/>/g]+/);
                        for (let i in amzOrderId) {
                            amzOrderId[i] = this.Trim(amzOrderId[i]);
                        }
                        amzOrderId = amzOrderId.filter(item => item.trim());
                        this.$ajax({
                            method: "post",
                            url:
                                this.GLOBAL.orderAndOverseaService +
                                "/orderMsg/getOrderMsg",
                            data: {
                                amzOrderIds: amzOrderId.join()
                            }
                        }).then(res => {
                            if (res.data.code === ERR_OK) {
                                this.loading = false;
                                this.itemData = res.data.data;
                                if (this.itemData.length === 0) {
                                    this.txt = this.$t("m.message.noMoreData");
                                }
                            }
                        });
                    }, 200);
                }
            });
        },
        Trim(str) {
            return str.replace(/(^\s*)|(\s*$)/g, "");
        }
    }
};
</script>
<style lang="stylus">
.orderDetailst-warp {
    .left {
        .el-textarea {
            .el-textarea__inner {
                border-radius: 0;
                resize: none;
            }
        }
    }
}
</style>
<style lang="stylus" scoped>
.orderDetailst-warp {
    min-height: 400px;

    .name {
        font-size: 16px;
        color: #333;
    }

    .left {
        float: left;
        width: 200px;

        .el-textarea {
            margin: 10px 0;

            .el-textarea__inner {
                border-radius: 0;
            }
        }

        .el-button {
            width: 92px;
        }
    }

    .right {
        padding-left: 220px;

        .name {
            margin-bottom: 10px;
        }

        .null {
            padding: 50px;
            text-align: center;
            font-size: 16px;
            color: #606266;
        }

        .content {
            border: 1px solid #e6e6e6;
            padding: 20px;
            margin-bottom: 20px;
            background: #f5f5f5;

            p {
                padding: 3px 0;
                color: #606266;
                font-size: 14px;

                span {
                    margin-right: 20px;
                }
            }

            p.title {
                padding: 5px 0;
                color: #333;
                font-weight: bold;
            }
        }
    }
}
</style>

                                      